package com.hngc.Search;


import com.alibaba.fastjson.*;
//import com.alibaba.fastjson.JSONObject;

public class text {
    public JSONObject[] JSON_res(String res) {
        JSONArray res_JSON = JSONArray.parseArray(res);
        JSONObject[] obj_res = new JSONObject[res_JSON.size()];
        for (int i = 0; i < res_JSON.size(); i++) {
            obj_res[i] = (JSONObject) res_JSON.get(i);
        }
        return obj_res;
    }

    public boolean PointInPolygon(String point, String polygon) {
        JSONObject[] JSON_point = this.JSON_res("[" + point + "]");                        //测试点
        double point_res_x = Double.parseDouble(JSON_point[0].getString("x"));    //测试点的x
//		System.out.println(point_res_x);
        double point_res_y = Double.parseDouble(JSON_point[0].getString("y"));    //测试点的y
//		System.out.println(point_res_y);
        JSONObject[] JSON_polygon = this.JSON_res(polygon);                        //测试面积
        int N = JSON_polygon.length;                                        //测试面积点的数量
        boolean boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
        int intersectCount = 0; //cross points count of x
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容

        JSONObject polygon_01 = JSON_polygon[0];        //面的第一个点
        double polygon_01_x = Double.parseDouble(polygon_01.getString("x"));    //测试点2的x
//		System.out.println(polygon_01_x);
        double polygon_01_y = Double.parseDouble(polygon_01.getString("y"));    //测试点2的y
//		System.out.println(polygon_01_y);
        for (int i = 1; i <= N; ++i) { //check all rays
            if (point_res_x == polygon_01_x && point_res_y == polygon_01_y) {
                return boundOrVertex; //p is an vertex
            }
            JSONObject polygon_02 = JSON_polygon[i % N]; //right vertex
            double polygon_02_x = Double.parseDouble(polygon_02.getString("x"));    //测试点2的x
//			System.out.println(polygon_02_x);
            double polygon_02_y = Double.parseDouble(polygon_02.getString("y"));    //测试点2的y
//			System.out.println(polygon_02_y);
            if (point_res_y < Math.min(polygon_01_y, polygon_02_y) || point_res_y > Math.max(polygon_01_y, polygon_02_y)) { //ray is outside of our interests
                polygon_01 = polygon_02;
                continue; //next ray left point
            }

            if (point_res_y > Math.min(polygon_01_y, polygon_02_y) && point_res_y < Math.max(polygon_01_y, polygon_02_y)) { //ray is crossing over by the algorithm (common part of)
                if (point_res_x <= Math.max(polygon_01_x, polygon_02_x)) { //x is before of ray
                    if (polygon_01_y == polygon_02_y && point_res_x >= Math.min(polygon_01_x, polygon_02_x)) { //overlies on a horizontal ray
                        return boundOrVertex;
                    }

                    if (polygon_01_x == polygon_02_x) { //ray is vertical
                        if (polygon_01_x == point_res_x) { //overlies on a vertical ray
                            return boundOrVertex;
                        } else { //before ray
                            ++intersectCount;
                        }
                    } else {//cross point on the left side
                        double xinters = (point_res_y - polygon_01_y) * (polygon_02_x - polygon_01_x) / (polygon_02_y - polygon_01_y) + polygon_01_x; //cross point of x
                        if (Math.abs(point_res_x - xinters) < precision) { //overlies on a ray
                            return boundOrVertex;
                        }

                        if (point_res_x < xinters) { //before ray
                            ++intersectCount;
                        }
                    }
                }
            } else {    //special case when ray is crossing through the vertex
                if (point_res_y == polygon_02_y && point_res_x <= polygon_02_x) { //p crossing over p2
                    JSONObject polygon_03 = JSON_polygon[(i + 1) % N]; //next vertex
                    double polygon_03_x = Double.parseDouble(polygon_03.getString("x"));    //测试点2的x
                    double polygon_03_y = Double.parseDouble(polygon_03.getString("y"));    //测试点2的y
                    if (point_res_y >= Math.min(polygon_01_y, polygon_03_y) && point_res_y <= Math.max(polygon_01_y, polygon_03_y)) { //p.y lies between p1.y & p3.y
                        ++intersectCount;
                    } else {
                        intersectCount += 2;
                    }
                }
            }
            polygon_01 = polygon_02;

        }
        if (intersectCount % 2 == 0) { //偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }
    }

    public static void main(String[] args) {
        String p = "{ x:7 , y :8}";
        String pp = "{x:112.97864198775136,y:28.199261275149492}";
        String po = "[{\"x\":112.97703536711337,\"y\":28.200830225296613},{\"x\":112.9770197678185,\"y\":28.200252211634854},{\"x\":112.97698735854772,\"y\":28.199902399458615},{\"x\":112.9769538803055,\"y\":28.199481580103225},{\"x\":112.976753384199,\"y\":28.19836603472614},{\"x\":112.97668565619641,\"y\":28.197746476348232},{\"x\":112.9793756058888,\"y\":28.197525843003536},{\"x\":112.98028896153843,\"y\":28.197477238194463},{\"x\":112.98033336116411,\"y\":28.197839070444115},{\"x\":112.98036904691484,\"y\":28.199560284570325},{\"x\":112.9803952817434,\"y\":28.200788441593172},{\"x\":112.98038532682111,\"y\":28.200848512813643},{\"x\":112.98035933448554,\"y\":28.2008936711711},{\"x\":112.98029127026449,\"y\":28.20092706735811},{\"x\":112.97736526091646,\"y\":28.201037525414502},{\"x\":112.97720088886541,\"y\":28.20100587776696},{\"x\":112.97710161610898,\"y\":28.200940693476518},{\"x\":112.97703536711337,\"y\":28.200830225296613}]";
        text Text = new text();
        JSONObject[] p_p = Text.JSON_res("[" + p + "]");
        JSONObject pp_p = p_p[0];
        String p_pp = p_p[0].toString();
//		double p_x = Double.parseDouble(p_p[0].getString("x"));
//		System.out.println(p_pp);
//		System.out.println(p_x);
        System.out.println("是否在面积内呢？？ 答案是" + Text.PointInPolygon(pp, po));
    }


}
